package com.example.demo.services.impl;

import com.example.demo.domain.Item;
import com.example.demo.domain.LineItem;
import com.example.demo.domain.Order;
import com.example.demo.domain.Sequence;
import com.example.demo.persistence.ItemDao;
import com.example.demo.persistence.LineItemDao;
import com.example.demo.persistence.OrderDao;
import com.example.demo.persistence.SequenceDao;
import com.example.demo.services.OrderService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("orderService")
@MapperScan("com.example.demo.persistence")
@MapperScan("com.example.demo.domain")
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private LineItemDao lineItemDao;

    @Autowired
    private ItemDao itemDao;

    @Autowired
    private SequenceDao sequenceDao;

    @Override
    public int insertOrder(Order order){
        order.setOrderId(getNextId("ordernum"));
        System.out.println("创建订单："+order.getOrderId());

        for(int i=0;i<order.getLineItems().size();i++){
            LineItem lineItem = order.getLineItems().get(i);
            String itemId = lineItem.getItemId();
            int increment = lineItem.getQuantity();
            Map<String, Object> param = new HashMap<String, Object>(2);
            param.put(itemId,increment);
            itemDao.updateInventoryQuantity(param);
        }
        String temp = new SimpleDateFormat("yyyyMMdd").format(order.getOrderDate())+ ((int) (Math.random() * 9000) + 1000);
//        while (orderDao.getOrderByNum(temp) != null){
//            temp = new SimpleDateFormat("yyyyMMdd").format(order.getOrderDate())+ String.valueOf((int) (Math.random() * 9000) + 1000);
//        }
        order.setOrderNum(temp);
        System.out.println(order);
        orderDao.insertOrder(order);
        orderDao.insertOrderStatus(order);
        for (int i = 0; i < order.getLineItems().size(); i++) {
            LineItem lineItem = (LineItem) order.getLineItems().get(i);
            lineItem.setOrderId(order.getOrderId());
            lineItemDao.insertLineItem(lineItem);
        }
        return order.getOrderId();
    }

    @Override
    public Order getOrder(int orderId){
        Order order = orderDao.getOrder(orderId);
        order.setLineItems(lineItemDao.getLineItemsByOrderId(orderId));

        for (int i = 0; i < order.getLineItems().size(); i++) {
            LineItem lineItem = order.getLineItems().get(i);
            Item item = itemDao.getItem(lineItem.getItemId());
            item.setQuantity(itemDao.getInventoryQuantity(lineItem.getItemId()));
            lineItem.setItem(item);
        }

        return order;
    }

    @Override
    public List<Order> getOrdersByUsername(String username){
        return orderDao.getOrdersByUsername(username);
    }

    @Override
    public int getNextId(String name){
        Sequence sequence = new Sequence(name, -1);
        sequence = (Sequence) sequenceDao.getSequence(sequence);
        if (sequence == null) {
            throw new RuntimeException("Error: A null sequence was returned from the database (could not get next " + name
                    + " sequence).");
        }
        Sequence parameterObject = new Sequence(name, sequence.getNextId() + 1);
        sequenceDao.updateSequence(parameterObject);
        return sequence.getNextId();
    }
}
